package org.jeecg.modules.ldwx.channel.service.impl;

import org.jeecg.modules.ldwx.channel.entity.TestCpMain;
import org.jeecg.modules.ldwx.channel.entity.TestCpInterface;
import org.jeecg.modules.ldwx.channel.entity.TestCpError;
import org.jeecg.modules.ldwx.channel.mapper.TestCpInterfaceMapper;
import org.jeecg.modules.ldwx.channel.mapper.TestCpErrorMapper;
import org.jeecg.modules.ldwx.channel.mapper.TestCpMainMapper;
import org.jeecg.modules.ldwx.channel.service.ITestCpMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 渠道信息
 * @Author: jeecg-boot
 * @Date:   2025-07-01
 * @Version: V1.0
 */
@Service
public class TestCpMainServiceImpl extends ServiceImpl<TestCpMainMapper, TestCpMain> implements ITestCpMainService {

	@Autowired
	private TestCpMainMapper testCpMainMapper;
	@Autowired
	private TestCpInterfaceMapper testCpInterfaceMapper;
	@Autowired
	private TestCpErrorMapper testCpErrorMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(TestCpMain testCpMain, List<TestCpInterface> testCpInterfaceList,List<TestCpError> testCpErrorList) {
		testCpMainMapper.insert(testCpMain);
		if(testCpInterfaceList!=null && testCpInterfaceList.size()>0) {
			for(TestCpInterface entity:testCpInterfaceList) {
				//外键设置
				entity.setInterfaceFkId(testCpMain.getId());
				testCpInterfaceMapper.insert(entity);
			}
		}
		if(testCpErrorList!=null && testCpErrorList.size()>0) {
			for(TestCpError entity:testCpErrorList) {
				//外键设置
				entity.setErrorFkId(testCpMain.getId());
				testCpErrorMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(TestCpMain testCpMain,List<TestCpInterface> testCpInterfaceList,List<TestCpError> testCpErrorList) {
		testCpMainMapper.updateById(testCpMain);
		
		//1.先删除子表数据
		testCpInterfaceMapper.deleteByMainId(testCpMain.getId());
		testCpErrorMapper.deleteByMainId(testCpMain.getId());
		
		//2.子表数据重新插入
		if(testCpInterfaceList!=null && testCpInterfaceList.size()>0) {
			for(TestCpInterface entity:testCpInterfaceList) {
				//外键设置
				entity.setInterfaceFkId(testCpMain.getId());
				testCpInterfaceMapper.insert(entity);
			}
		}
		if(testCpErrorList!=null && testCpErrorList.size()>0) {
			for(TestCpError entity:testCpErrorList) {
				//外键设置
				entity.setErrorFkId(testCpMain.getId());
				testCpErrorMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		testCpInterfaceMapper.deleteByMainId(id);
		testCpErrorMapper.deleteByMainId(id);
		testCpMainMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			testCpInterfaceMapper.deleteByMainId(id.toString());
			testCpErrorMapper.deleteByMainId(id.toString());
			testCpMainMapper.deleteById(id);
		}
	}
	
}
